//	CP2013 Traffic Simulator Assignment 2
//      Traffic Model PositionArea
//	Java version
//      Coded by Matthew Johnson


package Traffic;

//PositionArea stores coordinates and a xLength and yLength
public class PositionArea
{
    public int x;
    public int y;
    public int xLength;
    public int yLength;

    //Constructor:
    PositionArea(int a, int b, int l, int w)
    {
        x = a;
        y = b;
        xLength = l;
        yLength = w;
    }

    //Copy Constructor
    PositionArea(PositionArea other)
    {
        x = other.x;
        y = other.y;
        xLength = other.xLength;
        yLength = other.yLength;
    }

    //Comparison function
    public boolean equals(PositionArea other)
    {
        return(x == other.x && y == other.y && xLength == other.xLength && yLength == other.yLength);
    }

    //Tests if 2 PositionAreas are overlapping
    public boolean overlaps (PositionArea other)
    {

        //The return value
        boolean retVal = true;

        //If other's left edge is to the right of this' right edge, then other is completely to this' right
        if (other.x > this.x + this.xLength - 1)
            retVal = false;

        //If other's right edge is to the left of this' left edge, then other is completely to the left
        if (retVal && other.x + other.xLength - 1 < this.x)
            retVal = false;

        //If other's top is below this' bottom, other is completely below
        if (retVal && this.y + this.yLength - 1 < other.y)
            retVal = false;

        //If other's bottom is completely above this' top, other is above
        if (retVal && other.y + other.yLength - 1 < this.y)
            retVal = false;

        return retVal;
    }

    //Makes this instance's values equal a target instance's
    void copy(PositionArea p)
    {
        this.x = p.x;
        this.y = p.y;
        this.xLength = p.xLength;
        this.yLength = p.yLength;
    }

}
